<?php
/**
 *
 * @author Rodrigo Régis Palmeira <regisbsb@gmail.com>
 * @version 0.1
 *
 */
namespace Foxforms;
class Element
{
	protected $name;
	protected $id;
	protected $type;
	protected $label;
	protected $class	= array();
	protected $style	= array();
	protected $event	= array();
	protected $element	= array();

	/**
	 * Set the type of a input
	 * @param string $value
	 * @return FoxInputForms
	 */
	public function setType($type)
	{
		$this->type = $type;
		return $this;
	}

	public function setLabel($label)
	{
		$this->label = $label;
		return $this;
	}

	public function get($attribute)
	{
		return $this->$attribute;
	}

	/**
	 * Set the name attribute
	 * @param string $name
	 * @return FoxElementForm
	 */
	public function setName($name)
	{
		$this->name = $name;

		if (!isset($this->id))
			$this->setId('id_'.$name);
		return $this;
	}

	/**
	 * Set the class attribute
	 * @param string $id
	 * @return FoxElementForm
	 */
	public function setId($id)
	{
		$this->id = $id;
		return $this;
	}

	/**
	 * Add classes for the element
	 * @param array/string $class
	 * @return FoxElementForm
	 */
	public function addClass($class)
	{
		if (!is_array($class))
			$class = array($class);

		$this->class = array_merge($class, $this->class);
		return $this;
	}

	public function addStyle($style)
	{
		if (!is_array($style))
			$style = array($style);

		$this->style = array_merge($style, $this->style);
		return $this;
	}

	/**
	 * Add event's for the element
	 * @param array(event => action) $event
	 * @return FoxElementForm
	 */
	public function addEvent($event, $action)
	{
		$this->event[$event][]	= $action;
		return $this;
	}

	/**
	 * Add an element in a form
	 * @param FoxElementForms $element
	 * @return FoxFormForms
	 */
	public function addElement(FoxElementForms $element)
	{
		$this->element[] = $element;
		return $this;
	}

	public function label($content)
	{
		return FoxLabelForms::create($content)->setFor($this->get('id'));
	}

	public function generateForm(\Doctrine_Table $table)
	{
		$form = new \stdClass();

		foreach ($table->getColumns() as $key => $param)
		{
			$form->$key = \FoxForms\FoxFieldForms::createField($key, $param);
		}

		return $form;
	}

	/**
	 * Render the html output
	 * @return string
	 */
	public function render()
	{
		$html	= ' ';

		if ($this instanceof FoxFormForms)
			$html	.= '<form';
		else if ($this instanceof FoxInputForms)
			$html	.= '<input';
		else if ($this instanceof FoxSelectForms)
			$html	.= '<select';
		else if ($this instanceof FoxTextAreaForms)
			$html	.= '<textarea';
		else if ($this instanceof FoxDivForms)
			$html	.= '<div';
		else if ($this instanceof FoxLabelForms)
			$html	.= '<label';
		else if ($this instanceof FoxAForms)
			$html	.= '<a';
		else if ($this instanceof FoxButtonForms)
			$html	.= '<button';
		else if ($this instanceof FoxFieldsetForms)
			$html	.= '<fieldset';
		else if ($this instanceof FoxLegendForms)
			$html	.= '<legend';


		if (!empty($this->name))
			$html	.= " name='{$this->name}'";
		if (!empty($this->type))
			$html	.= " type='{$this->type}'";
		if (!empty($this->value))
			$html	.= " value='{$this->value}'";
		if (!empty($this->maxlength))
			$html	.= " maxlength='{$this->maxlength}'";
		if (!empty($this->size))
			$html	.= " size='{$this->size}'";
		if (!empty($this->readOnly))
			$html	.= " readonly";
		if (!empty($this->disabled))
			$html	.= " disabled='{$this->disabled}'";
		if (!empty($this->id))
			$html	.= " id='{$this->id}'";
		if (!empty($this->for))
			$html	.= " for='{$this->for}'";
		if (!empty($this->action))
			$html	.= " action='{$this->action}'";
		if (!empty($this->method))
			$html	.= " method='{$this->method}'";
		if (!empty($this->target))
			$html	.= " target='{$this->target}'";
		if (!empty($this->enctype))
			$html	.= " enctype='{$this->enctype}'";
		if (!empty($this->multiple))
			$html	.= " multiple='{$this->multiple}'";
		if (!empty($this->cols))
			$html	.= " cols='{$this->cols}'";
		if (!empty($this->rows))
			$html	.= " rows='{$this->rows}'";
		if (!empty($this->href))
			$html	.= " href='{$this->href}'";

		if (count($this->class))
		{
			$class = implode(' ',$this->class);
			$html.= " class='{$class}'";
		}

		if (count($this->style))
		{
			$style = implode('; ',$this->style);
			$html.= " style=\"{$style}\"";
		}

		if (count($this->event))
		{
			foreach ($this->event as $event => $action)
			{
				$action = implode(';',$action);
				$html.= " {$event}=\"{$action}\"";
			}
		}
		$html	.= '/>';

		foreach ($this->element as $element)
		{
			$html.= $element->render();
		}

		if ($this instanceof FoxSelectForms)
		{
			foreach ($this->option as $key => $value)
			{
				$selected = null;
				if (is_array($this->selected))
				{
					if (in_array($key, $this->selected))
						$selected = "selected='selected'";
				}
				else if ($this->selected == $key)
					$selected = "selected='selected'";
				$html.= "<option value='{$key}' {$selected}>{$value}</option>";
			}
		}

		if (!empty($this->text))
			$html	.= "{$this->text}";

		if (!empty($this->content))
			$html	.= "{$this->content}";

		if ($this instanceof FoxSelectForms)
			$html	.= '</select>';
		else if ($this instanceof FoxTextAreaForms)
			$html	.= '</textarea>';
		else if ($this instanceof FoxFormForms)
			$html	.= '</form>';
		else if ($this instanceof FoxDivForms)
			$html	.= '</div>';
		else if ($this instanceof FoxLabelForms)
			$html	.= '</label>';
		else if ($this instanceof FoxAForms)
			$html	.= '</a>';
		else if ($this instanceof FoxButtonForms)
			$html	.= '</button>';
		else if ($this instanceof FoxFieldsetForms)
			$html	.= '</fieldset>';
		else if ($this instanceof FoxLegendForms)
			$html	.= '</legend>';

		return $html;
	}

}